package com.core;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.bean.ChatBean;
import com.bean.PersonBean;
import com.bean.SessionBean;
import com.business.ChatBusiness;
import com.service.FeedbackService;
import com.service.UserService;


public class AjaxHandleMethod {
	
	public static void getSessionByStaff(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		PersonBean person = (PersonBean) request.getSession().getAttribute("person");
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int rows = Integer.parseInt(request.getParameter("pageSize"));
		int offset = (pageIndex-1) * rows;
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		ChatBusiness business = (ChatBusiness) wctx.getBean("feedbackBusiness");
		String json = business.getSessionByStaff(person.getEname(), offset, rows);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json);
	}
	
	public static void getSessionByDirector(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int rows = Integer.parseInt(request.getParameter("pageSize"));
		int offset = (pageIndex-1) * rows;
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		ChatBusiness business = (ChatBusiness) wctx.getBean("feedbackBusiness");
		String json = business.getSessionByDirector(offset, rows);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json);
	}
	
	public static void getPublicSession(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int rows = Integer.parseInt(request.getParameter("pageSize"));
		int offset = (pageIndex-1) * rows;
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		ChatBusiness business = (ChatBusiness) wctx.getBean("feedbackBusiness");
		String json = business.getPublicSession(offset, rows);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json);
	}
	
	public static void launchFeedback(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		String topic = request.getParameter("topic");
		String content = request.getParameter("content");
		String isPublic = request.getParameter("public");
		PersonBean person = (PersonBean) request.getSession().getAttribute("person");
		SessionBean sessionBean = new SessionBean();
		sessionBean.setTopic(topic);
		sessionBean.setCname(person.getCname());
		sessionBean.setEname(person.getEname());
		sessionBean.setIsPublic(isPublic);
		sessionBean.setIsReadD("1");
		sessionBean.setIsReadS("0");
		ChatBean chatBean = new ChatBean();
		chatBean.setCname(person.getCname());
		chatBean.setEname(person.getEname());
		chatBean.setContent(content);
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		FeedbackService feedbackService = (FeedbackService) wctx.getBean("feedbackTarget");
		int ret = feedbackService.launchFeedback(sessionBean, chatBean);
		response.getWriter().write(String.valueOf(ret));
	}
	
	public static void queryChatDetail(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		int sessionid = Integer.parseInt(request.getParameter("sessionid"));
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		ChatBusiness chatBusiness = (ChatBusiness) wctx.getBean("chatBusiness");
		int role = Integer.parseInt(((PersonBean)request.getSession().getAttribute("person")).getRole());
		String json = chatBusiness.queryChatDetail(sessionid, role);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json);
	}
	
	public static void reply(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		int sessionid = Integer.parseInt(request.getParameter("sessionid"));
		String content = request.getParameter("textareaContent");
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		FeedbackService feedbackService = (FeedbackService) wctx.getBean("feedbackTarget");
		PersonBean person = (PersonBean) request.getSession().getAttribute("person");
		ChatBean chatBean = new ChatBean();
		chatBean.setSessionID(sessionid);
		chatBean.setContent(content);
		chatBean.setCname(person.getCname());
		chatBean.setEname(person.getEname());
		int role = Integer.parseInt(((PersonBean)request.getSession().getAttribute("person")).getRole());
		int ret = feedbackService.reply(sessionid, role, chatBean);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(String.valueOf(ret));
	}
	
	public static void delTopic(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		int sessionid = Integer.parseInt(request.getParameter("sessionid"));
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		FeedbackService feedbackService = (FeedbackService) wctx.getBean("feedbackTarget");
		int ret = feedbackService.delTopic(sessionid);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(String.valueOf(ret));
	}
	
	public static void changePassword(HttpServletRequest request, HttpServletResponse response, ServletContext sc) throws ClassNotFoundException, SQLException, IOException{
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		String rePassword = request.getParameter("rePassword");
		String ename = ((PersonBean)request.getSession().getAttribute("person")).getEname();
		
		WebApplicationContext wctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		UserService service  = (UserService) wctx.getBean("userTarget");
		boolean isCorrect = service.hasMatchUser(ename, oldPassword);
		int ret = 0;
		if(isCorrect){
			if(newPassword.equals(rePassword)){
				ret = service.updatePassword(ename, newPassword);
			}
		}else{
			ret = -1;
		}
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(String.valueOf(ret));
	}
}
